﻿using System;
using System.Collections.Generic;

namespace Pfz.RemoteGaming
{
	/// <summary>
	/// Class that manages animations.
	/// Note that animations are done as IEnumerators, so you can program loops sequentially and use yield return
	/// to wait for the next frame.
	/// </summary>
	public sealed class RemoteGameAnimationManager
	{
		private List<IEnumerator<bool>> _animations = new List<IEnumerator<bool>>();
		public void Run(IEnumerator<bool> animation)
		{
			if (animation == null)
				throw new ArgumentNullException("animation");

			_animations.Add(animation);
		}

		/// <summary>
		/// Runs all possible animations and returns true if more animations can be processed.
		/// Note that it will always return true if there are no animations.
		/// </summary>
		internal void _Run()
		{
			int count = _animations.Count;
			for(int i=count-1; i>=0; i--)
			{
				var animation = _animations[i];

				if (!animation.MoveNext())
				{
					animation.Dispose();
					_animations.RemoveAt(i);
					continue;
				}
			}
		}
	}
}
